import json
import os
import SimpleITK as sitk
import numpy as np
import matplotlib.pyplot as plt


def visual_slice(images, images_name):
    plt.imsave(images_name, images, cmap='gray', dpi=150)

def visual_hot_map(images, images_name):
    plt.imsave(images_name, images, cmap='RdBu_r', dpi=150)
    
def visual_seg(images, images_name):
    plt.imsave(images_name, images, cmap='gray', dpi=150)

def read_nii(path):
    image = sitk.ReadImage(path)
    image = sitk.GetArrayFromImage(image)
    return image     

def norm(image):
    return (image - np.min(image)) / (np.max(image) - np.min(image))   

if __name__ == '__main__':
    t1_path = '/Users/qlc/Desktop/Dataset/Brats2021/TrainingData/BraTS2021_00018/BraTS2021_00018_t1.nii.gz'
    t1ce_path = '/Users/qlc/Desktop/Dataset/Brats2021/TrainingData/BraTS2021_00018/BraTS2021_00018_t1ce.nii.gz'
    t2_path = '/Users/qlc/Desktop/Dataset/Brats2021/TrainingData/BraTS2021_00018/BraTS2021_00018_t2.nii.gz'
    flair_path = '/Users/qlc/Desktop/Dataset/Brats2021/TrainingData/BraTS2021_00018/BraTS2021_00018_flair.nii.gz'
    seg_path = '/Users/qlc/Desktop/Dataset/Brats2021/TrainingData/BraTS2021_00018/BraTS2021_00018_seg.nii.gz'
    
    t1 = read_nii(t1_path)
    t2 = read_nii(t2_path)
    flair = read_nii(flair_path)
    t1ce = read_nii(t1ce_path)
    seg = read_nii(seg_path)
    
    t1_n = norm(t1)
    t2_n = norm(t2)
    flair_n = norm(flair)
    t1ce_n = norm(t1ce)
    
    seg_1 = seg==1
    seg_2 = seg==2
    seg_4 = seg==4
        
    # # slice 86
    visual_hot_map(norm(np.abs(t1ce_n - 1) - t1_n)[86], './t1-t1ce.png')
    visual_hot_map(norm(flair - np.abs(t2 - 1))[86], './flair-t2.png')
    visual_hot_map(norm(t1 - np.abs(t2 - 1))[86], './t1-t2.png')
    
    visual_slice(norm(t1_n - np.abs(t1ce_n - 1))[86] > 0.55, './t1-t1ce-0.55.png')
    visual_slice(norm(flair - np.abs(t2 - 1))[86] > 0.75, './flair-t2-0.55.png')
    visual_slice(norm(t1 - np.abs(t2 - 1))[86] > 0.5, './t1-t2-0.55.png')
    
    print(np.max(norm(t1_n - np.abs(t1ce_n - 1))[86]), np.min(norm(t1_n - np.abs(t1ce_n - 1))[86]))
    
    # # slice 100
    # visual_hot_map(norm(t1_n - np.abs(t1ce_n - 1))[100], './t1-t1ce.png')
    # visual_hot_map(norm(flair - np.abs(t2 - 1))[100], './flair-t2.png')
    # visual_hot_map(norm(t1 - np.abs(t2 - 1))[100], './t1-t2.png')
    
    # visual_slice(norm(t1_n - np.abs(t1ce_n - 1))[100] > 0.55, './t1-t1ce-0.55.png')
    # visual_slice(norm(flair - np.abs(t2 - 1))[100] > 0.75, './flair-t2-0.55.png')
    # visual_slice(norm(t1 - np.abs(t2 - 1))[100] > 0.5, './t1-t2-0.55.png')

    
    # visual_slice(np.power(norm(np.abs(t1ce_n - 1) - t1_n)[100], 2) > 0.99, './b.png')
    
    # visual_hot_map(norm(flair_n - t2_n)[100], './b.png')
    
    
    
    
    
    
    
    
    
    

    

    